package com.jk.vehicle.core.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jk.vehicle.core.entity.TableResultResponse;
import com.jk.vehicle.core.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * 通用业务类
 *
 * @author KangJian
 * @date 2018/6/15
 */
public abstract class AbstractBizService<M extends Mapper<T>, T> {
    /**
     * 通用mapper
     */
    @Autowired
    protected M mapper;

    public void setMapper(M mapper) {
        this.mapper = mapper;
    }

    /**
     * 单个查询
     * @param entity
     * @return
     */
    public T selectOne(T entity) {
        return mapper.selectOne(entity);
    }

    /**
     * ID查询
     * @param id
     * @return
     */
    public T selectById(Object id) {
        return mapper.selectByPrimaryKey(id);
    }


    /**
     * 列表查询
     * @param entity
     * @return
     */
    public List<T> selectList(T entity) {
        return mapper.select(entity);
    }


    /**
     * 全部查询
     * @return
     */
    public List<T> selectListAll() {
        return mapper.selectAll();
    }


    /**
     * 计数
     * @param entity
     * @return
     */
    public Long selectCount(T entity) {
        return new Long(mapper.selectCount(entity));
    }


    /**
     * 新增
     * @param entity
     */
    public void insert(T entity) {
        mapper.insert(entity);
    }


    /**
     * 选择性新增
     * @param entity
     */
    public void insertSelective(T entity) {
        mapper.insertSelective(entity);
    }


    /**
     * 删除
     * @param entity
     */
    public void delete(T entity) {
        mapper.delete(entity);
    }


    /**
     * ID删除
     * @param id
     */
    public void deleteById(Object id) {
        mapper.deleteByPrimaryKey(id);
    }


    /**
     * 更新
     * @param entity
     */
    public void updateById(T entity) {
        mapper.updateByPrimaryKey(entity);
    }

    /**
     * 更新
     * @param entity
     */
    public void updateSelectiveById(T entity) {
        mapper.updateByPrimaryKeySelective(entity);

    }

    /**
     * 查询
     * @param example
     * @return
     */
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    /**
     * 查询
     * @param example
     * @return
     */
    public int selectCountByExample(Object example) {
        return mapper.selectCountByExample(example);
    }

    /**
     * 查询
     * @param query
     * @return
     */
    public TableResultResponse<T> selectByQuery(Query query) {
        Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        Example example = new Example(clazz);
        if (query.entrySet().size() > 0) {
            Example.Criteria criteria = example.createCriteria();
            for (Map.Entry<String, Object> entry : query.entrySet()) {
                criteria.andLike(entry.getKey(), "%" + entry.getValue().toString() + "%");
            }
        }
        Page<Object> result = PageHelper.startPage(query.getPage(), query.getLimit());
        List<T> list = mapper.selectByExample(example);
        return new TableResultResponse<T>(result.getTotal(), list);
    }

    /**
     * 查询
     * @param query
     * @return
     */
    public TableResultResponse<T> selectByKey(Query query) {
        Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        Example example = new Example(clazz);
        Example.Criteria criteria1 = example.createCriteria();
        Example.Criteria criteria2 = example.createCriteria();
        String key = "";

        if (null != query.get("key")) {
            key = query.get("key").toString();
        }
        criteria1.andLike("name", "%" + key + "%");
        criteria2.andLike("code", "%" + key + "%");
        example.or(criteria2);

        Page<Object> result = PageHelper.startPage(query.getPage(), query.getLimit());
        List<T> list = mapper.selectByExample(example);
        return new TableResultResponse<T>(result.getTotal(), list);
    }

}
